package com.gold.zookeeperdemo.lock;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.locks.Lock;

/**
 *  zk分布式锁-临时顺序节点实现版-测试
 */
public class ZkDistributedLockTest {
    private static Lock LOCK=new ZkDistributedLock();

    public static void execuBusiness(){
        try{
            LOCK.lock();
            System.out.println(Thread.currentThread().getName()+"执行业务代码");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            LOCK.unlock();
        }
    }

    public static void main(String[] args) {
        //循环屏障
        int current=20;
        CyclicBarrier cyclicBarrier=new CyclicBarrier(current);

        for (int i = 0; i < current ; i++) {
            new Thread(() -> {
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
                System.out.println("我出发了-----------");
                ZkDistributedLockTest.execuBusiness();
            }).start();
        }
    }
}
